<?php
/**
 *  public functions for use
 *
 */
//log level
if (!defined("ERROR"))
{
    define("ERROR", "ERROR");
}
if (!defined("WARN"))
{
    define("WARN", "WARN");
}
if (!defined("DEBUG"))
{
    define("DEBUG", "DEBUG");
}
if (!defined("INFO"))
{
    define("INFO", "INFO");
}

function NICED_PRINT()
{
    $args = func_get_args();

    foreach ($args as $arg)
    {
	    FS::pout($arg);
    }
}

function NICED_LOG($level = DEBUG, $message, $prefix = NULL, $caller = true)
{
    $map   = Logger::$levelmap;
    $level = array_search($level, $map);

    if (false === $level)
    {
        return false;
    }

    $log_level    = array_search(SC::get("board_config.log_level"), $map);

    if ($level > $log_level)
    {
        return false;
    }

    $log_counter = SC::get("board_config.log_counter") + 1;
    SC::set("board_config.log_counter", $log_counter);

    if (is_array($message))
    {
        $message = var_export($message, true);
    }
    $message = $prefix." ".$message;

    $log_type     = SC::get("board_config.log_type");
    $log_filepath = SC::get("board_config.log_filepath");
    $log_filename_prefix = SC::get("board_config.log_filename_prefix");
    $log_filename_format = SC::get("board_config.log_filename_format");

	$logger = Logger::getInstance($log_level, $log_type, $log_filepath, $log_filename_prefix, $log_filename_format);

    if ($caller)
    {
        $content = $message." ".Util::getCaller(); 
    }
    else
    {
        $content = $message;
    }

    $content.= " [RequestID ".SC::get("board_config.request_id")." ".$log_counter."]";

	$logger->write($level, $content);
}

function NICED_TRACE($message)
{
    if (!$message)
    {
        return true;
    }

    $trace_info = SC::get("board_config.trace_info");
    $trace_info[] = $message;
    SC::set("board_config.trace_info", $trace_info);

    return true;
}

function NICED_TIME_START($namespace, $time = NULL)
{
    $namespace = $namespace ? ".".$namespace : $namespace;
    $key = "board_config.runtime_time.items".$namespace.".start";

    $time = $time ? $time : microtime(true);
    SC::set($key, $time);
}

function NICED_TIME_STOP($namespace, $stop_key = "end", $time = NULL)
{
    $n = $namespace;
    $namespace = $namespace ? ".".$namespace : $namespace;
    $key = "board_config.runtime_time.items".$namespace.".stop.".$stop_key;

    $time = $time ? $time : microtime(true);
    SC::set($key, $time);
    NICED_TIME_ELAPSE($n, $stop_key);
}

function NICED_TIME_ELAPSE($namespace, $stop_key = "end", $unit = 1)
{
    $namespace = $namespace ? ".".$namespace : $namespace;

    $key       = "board_config.runtime_time.items".$namespace.".elapse.".$stop_key;

    $elapse_time = SC::get($key);
    if ($elapse_time)
    {
        return $elapse_time;
    }

    $key_start = "board_config.runtime_time.items".$namespace.".start";
    $key_stop  = "board_config.runtime_time.items".$namespace.".stop.".$stop_key;

    $elapse_time = (SC::get($key_stop)- SC::get($key_start)) * $unit;
    SC::set($key, $elapse_time);

    return $elapse_time;
}

function NICED_MEMORY_START($namespace, $memory = NULL)
{
    $namespace = $namespace ? ".".$namespace : $namespace;
    $key = "board_config.runtime_memory.items".$namespace.".start";

    $memory = $memory ? $memory : memory_get_usage(); 
    SC::set($key, $memory);
}

function NICED_MEMORY_STOP($namespace, $stop_key = "end", $memory = NULL)
{
    $n = $namespace;
    $namespace = $namespace ? ".".$namespace : $namespace;
    $key = "board_config.runtime_memory.items".$namespace.".stop.".$stop_key;

    $memory = $memory ? $memory : memory_get_usage(); 
    SC::set($key, $memory);
    NICED_MEMORY_USAGE($n, $stop_key);
}

function NICED_MEMORY_USAGE($namespace, $stop_key = "end", $unit = 1)
{
    $namespace = $namespace ? ".".$namespace : $namespace;

    $key       = "board_config.runtime_memory.items".$namespace.".elapse.".$stop_key;

    $memory = SC::get($key);
    if ($memory )
    {
        return $memory;
    }

    $key_start = "board_config.runtime_memory.items".$namespace.".start";
    $key_stop  = "board_config.runtime_memory.items".$namespace.".stop.".$stop_key;

    $memory = (SC::get($key_stop)- SC::get($key_start)) * $unit;
    SC::set($key, $memory);

    return $memory;
}

function NICED_PAGE_START($page)
{
    SC::set("board_config.page.current", $page);

    return true;
}

function NICED_PAGE_GET()
{
    return SC::get("board_config.page.current");

    return true;
}

function NICED_ACCESS_LOG($k, $v = NULL)
{
    if (is_array($k))
    {
        SC::set("board_config.access_log.items", $k); 
    }
    else
    {
        SC::set("board_config.access_log.items.".$k, $v); 
    }

    return true;
}

function NICED_ALARM($key)
{
    //alarm
    if (class_exists("MonitorDispatcher"))
    {
        MonitorDispatcher::alarm($key);
    }

    return true;
}

